/*
 * Copyright 2010-2011 Mike Novak <michael.novakjr@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.codeversed.plugins.buzz.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

import com.larvalabs.slidescreen.PluginConstants;

import com.codeversed.plugins.buzz.util.BuzzUtils;
import com.codeversed.plugins.buzz.util.Constants;

public class BuzzContentProvider extends ContentProvider {
    public static final String AUTHORITY = "com.codeversed.plugins.buzz";
    public static final String CONTENT = "activities";
    
    public static final Uri CONTENT_URI = Uri.parse("content://" + BuzzContentProvider.AUTHORITY)
            .buildUpon().appendPath(BuzzContentProvider.CONTENT).build();
    
    @Override
    public boolean onCreate() {
        return true;
    }
    
    @Override
    public String getType(Uri uri) {
        return null;
    }
    
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        PowerManager powerManager = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock lock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.LOG_TAG);
        lock.acquire();
        
        SharedPreferences prefs = getContext().getSharedPreferences("buzz_oauth.prefs", Context.MODE_PRIVATE);
        
        if (!prefs.contains(Constants.PREF_TOKEN)) {
            return getNeedSettingsCursor();
        }
        
        Cursor cur = null;
        String token = prefs.getString(Constants.PREF_TOKEN, "anonymous");
        String tokenSecret = prefs.getString(Constants.PREF_TOKEN_SECRET, "anonymous");
                
        try {
            cur = BuzzUtils.requestConsumptionFeed(getContext(), token, tokenSecret);
        } catch (Exception e) {
            Log.e(Constants.LOG_TAG, "Exception: " + e.toString());
            return null;
        }
        
        if (lock != null && lock.isHeld()) {
            lock.release();
        }
        
        return cur;
    }
    
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }
    
    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return -1;
    }
    
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return -1;
    }
    
    private Cursor getNeedSettingsCursor() {
        MatrixCursor cursor = new MatrixCursor(new String[] { PluginConstants.FIELD_SETTINGS_NEEDED_MESSAGE });
        MatrixCursor.RowBuilder builder = cursor.newRow();
        builder.add("Press here to log into buzz");
        
        return cursor;
    }
}
